//定制排序
package daily.may0514;

import java.util.Comparator;
import java.util.TreeSet;

class Boy {
    private String name;
    private int age;
    public Boy(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return name + ":" + age;
    }
}

//定义实现类(太麻烦，不推荐)
class MyCompareRule implements Comparator<Boy>{
    @Override
    public int compare(Boy o1, Boy o2) {
        if (o1.getName().length() >= o2.getName().length()){
            return 1;//返回1为升序，返回-1为降序
        }
        return -1;
    }
}

public class TreeSetCustomSortTest {
    public static void main(String[] args) {
        //使用匿名内部类
        TreeSet<Boy> treeSet = new TreeSet<>(new Comparator<Boy>() {
            @Override
            public int compare(Boy boy1, Boy boy2) {
                if (boy1.getName().length() >= boy2.getName().length()) {
                    return 1;//返回1为升序，返回-1为降序
                }
                return -1;
            }
        });
        //使用Lambda表达式
        TreeSet<Boy> treeSetByLambda = new TreeSet<>((boy1, boy2) -> {
            if (boy1.getName().length() >= boy2.getName().length()) {
                return 1;
            }
            return -1;
        });
        //使用实现类（太麻烦，不推荐）
        TreeSet<Boy> treeSetByImp = new TreeSet<>(new MyCompareRule());

        treeSet.add(new Boy("Amy", 22));
        treeSet.add(new Boy("Bob", 19));
        treeSet.add(new Boy("Cloud", 19));
        treeSet.add(new Boy("DaMing", 21));
        treeSet.add(new Boy("Eve", 20));
        treeSet.add(new Boy("Franklin", 18));
        treeSet.add(new Boy("Gina", 21));
        System.out.println(treeSet);
    }
}
